import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lite_message/style/lm_icons.dart';

import 'package:lite_message/util/app_util.dart';

///输入框
class LMTextField extends StatelessWidget {
  const LMTextField({
    Key key,
    this.controller,
    this.suffixIcon,
    this.icon,
    this.maxLength,
    this.textInputAction = TextInputAction.done,
    this.keyboardType,
    this.hintText,
    this.onChanged,
    this.onEditingComplete,
    this.onTap,
    this.readOnly = false,
    this.autofocus = false,
    this.obscureText = false,
    this.radius,
    this.maxLines = 1,
    this.height,
    this.initialValue,
    this.inputFormatters,
    this.iconSize,
    this.focusNode,
  }) : super(key: key);

  final TextEditingController controller;

  ///末尾自定义控件
  final Widget suffixIcon;

  ///末尾图标
  final IconData icon;

  ///末尾图标大小
  final double iconSize;

  ///最大长度
  final int maxLength;

  ///输入模式
  final TextInputAction textInputAction;

  ///键盘模式
  final TextInputType keyboardType;

  ///提示文本
  final String hintText;

  final Function(String) onChanged;

  final Function() onEditingComplete;

  final Function() onTap;

  ///是否为只读
  final bool readOnly;

  ///自动获焦
  final bool autofocus;

  ///是否为密文
  final bool obscureText;

  ///圆角
  final double radius;

  ///最大行数
  final int maxLines;

  ///输入框高度
  final double height;

  ///默认值
  final String initialValue;

  ///输入正则
  final List<TextInputFormatter> inputFormatters;

  ///焦点控制器
  final FocusNode focusNode;

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: Theme.of(context)
          .copyWith(primaryColor: Theme.of(context).accentColor),
      child: Container(
        decoration: BoxDecoration(
          color: Theme.of(context).bottomAppBarColor,
          borderRadius: BorderRadius.circular(radius ?? 10),
        ),
        alignment: Alignment.center,
        height: height ?? 180.px,
        child: TextFormField(
          focusNode: focusNode,
          initialValue: initialValue,
          maxLines: maxLines,
          obscureText: obscureText,
          autofocus: autofocus,
          onChanged: onChanged,
          onEditingComplete: onEditingComplete,
          onTap: onTap,
          readOnly: readOnly,
          textInputAction: textInputAction,
          keyboardType: keyboardType,
          maxLength: maxLength,
          controller: controller,
          scrollPadding: EdgeInsets.zero,
          textAlignVertical: TextAlignVertical.center,
          cursorRadius: Radius.circular(20),
          cursorColor: Theme.of(context).accentColor,
          style: TextStyle(
            fontSize: 46.sp,
            color: Theme.of(context).accentColor.withOpacity(0.8),
            fontWeight: FontWeight.bold,
          ),
          decoration: InputDecoration(
            counterText: '',
            hintText: hintText ?? 'Hint text ...',
            hintStyle: TextStyle(color: Theme.of(context).disabledColor),
            contentPadding: EdgeInsets.symmetric(horizontal: 80.px),
            isDense: true,
            border: OutlineInputBorder(
              borderSide: BorderSide.none,
            ),
            suffixIcon: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: EdgeInsets.only(right: 50.px),
                  child: suffixIcon ??
                      Icon(icon ?? LmIcons.user, size: iconSize ?? 50.px),
                ),
              ],
            ),
          ),
          inputFormatters: inputFormatters,
        ),
      ),
    );
  }
}
